const weatherMap = {
  'sunny': '晴天',
  'cloudy': '多云',
  'overcast': '阴',
  'lightrain': '小雨',
  'heavyrain': '大雨',
  'snow': '雪'
}

const weatherColorMap = {
  'sunny': '#cbeefd',
  'cloudy': '#deeef6',
  'overcast': '#c6ced2',
  'lightrain': '#bdd5e1',
  'heavyrain': '#c5ccd0',
  'snow': '#aae1fc'
}


// 引入SDK核心类
const QQMapWX = require('../../libs/qqmap-wx-jssdk.js')
 
const UNPROMPTED=0;//未提示
const UNAUTHORIZED=1;//未授权
const AUTHORIZED=2;//授权

const UNPROMPTED_TIPS="点击获取当前位置"
const UNAUTHORIZED_TIPS="点击开启位置权限"
const AUTHORIZED_TIPS=""

Page({
  data:{
      nowTemp:'1000',
      nowWeather:'',
      nowBackground:'',
      hourlyWeather: [],
      todayDate:'',
      todayTemp:"",
      city:"广州市",
      locationTipsText:UNPROMPTED_TIPS,
      locationAuthType:UNPROMPTED
  },
  onLoad() {
    console.log("onLoadindex")
    this.qqmapsdk = new QQMapWX({
      key: 'IDBBZ-SUP6U-GI7VB-4OKOV-BC3P3-FZBHT'
    })
    this.getNow()
  },
onPullDownRefresh(){
  this.getNow(() => {
    wx.stopPullDownRefresh()
  })
},
getNow(callback){
  wx.request({
    url: 'https://test-miniprogram.com/api/weather/now', //仅为示例，并非真实的接口地址
    data: {
      city :this.data.city,
    },
    
    success: res => {
      let result = res.data.result
      this.setNow(result)
      this.setHourlyWeather(result)
      this.setToday(result)

      complete: ()=> {
      callback && callback()
        } 
    }
  })
},

setNow(result){
  let temp = result.now.temp
  let weather = result.now.weather
  this.setData({
    nowTemp: temp + '°',
    nowWeather: weatherMap[weather],
    nowBackground: '/images/' + weather + '-bg.png',
  })
  wx.setNavigationBarColor({
    frontColor: '#000000',
    backgroundColor: weatherColorMap[weather],
  })
},
setHourlyWeather(result){
  let nowHour = new Date().getHours()
  // 构建新的forecast列表
  let hourlyWeather = []
  // 获取网络列表
  let forecast = result.forecast
  for (let i = 0; i < 8; i += 1) {
    hourlyWeather.push({
      time: (i * 3 + nowHour) % 24 + '时',
      iconPath: '/images/' + forecast[i].weather + '-icon.png',
      temp: forecast[i].temp + '°'

    })
  }
  hourlyWeather[0].time = '现在'
  this.setData({
    hourlyWeather: hourlyWeather
  })
},
setToday(result){
  let date=new Date()
  this.setData({
      todayTemp: `${result.today.minTemp}° - ${result.today.maxTemp}°`,
      todayDate: `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} 今天`
  })
},
onTapDayWeather(){
  wx.navigateTo({
    url: '/pages/list/list?city=' + this.data.city
  })
},
onTapLocation(){
    this.getLocation()
},
getLocation(){
  if(this.data.locationAuthType===UNAUTHORIZED){
    wx.openSetting({
      
    })
  }else{
  wx.getLocation({
    success: res => {
      this.setData({
        locationAuthType:AUTHORIZED,
        locationTipsText:AUTHORIZED_TIPS
      })
      this.qqmapsdk.reverseGeocoder({
        location: {
          latitude: res.latitude,
          longitude: res.longitude
        },
        success: res => {
          let city = res.result.address_component.city
          this.setData({
            city: city,
            locationTipsText:""
          })
          this.getNow()
        }
      })      
    },fail:()=>{
      this.setData({
        locationAuthType:UNAUTHORIZED,
        locationTipsText:UNAUTHORIZED_TIPS
      })
    }
  })
  }
},

onShow(){
  console.log("onReadyindex")
}

});
